package com.lianzai.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import java.io.File;

/**
 * 在spring boot中加入相应配置的扫描
 * @author :艾超
 * @date :2017/12/5 11:06
 */
@Configuration
public class ApplicationConfig extends WebMvcConfigurerAdapter {

    @Value("${license.windows-path}")
    private String licenseWinPath;

    @Value("${license.linux-path}")
    private String licenseLinuxPath;

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        String osName = System.getProperty("os.name");
        String licensePath = licenseLinuxPath;
        if(osName.contains("Windows") || osName.contains("windows")){
            licensePath = licenseWinPath;
        }
        File licenseFile = new File(licensePath.substring(5));
        if(!licenseFile.exists()){
            licenseFile.mkdirs();
        }
        System.out.println("licensePath="+licensePath);
        registry.addResourceHandler("/license/**").addResourceLocations(licensePath);

        registry.addResourceHandler("/.well-known/pki-validation/**")
                .addResourceLocations("file:/data/java/.well-known/pki-validation/");

        registry.addResourceHandler("/ticket/*.txt")
                .addResourceLocations("file:/data/java/ticket/");

        super.addResourceHandlers(registry);
    }
}
